#!/bin/bash

#
APPNAME=blogapp
BASEDIR=$(cd $(dirname $0) && pwd)
APPROOT=$BASEDIR/$APPNAME
WORKDIR=$APPROOT/_work.$$
DBFILE=$APPROOT/db/dbfile
RANDOMTXT="1234567890123456asdfghjklLKJHGFDSA_____"
TREEFROG=treefrog
TSPAWN=tspawn
PORT=8885

# Function
row_count()
{
  echo "SELECT COUNT(1) FROM blog;" | sqlite3 $DBFILE
}

# Cleanup
cleanup()
{
  $TREEFROG -k abort $APPROOT >/dev/null 2>&1

  cd $BASEDIR
  rm -rf $WORKDIR
  rm -rf $APPNAME
}

testcase()
{
  # Start Check
  cd $APPROOT
  $TREEFROG -p $PORT -d || exit
  sleep 1
  $TREEFROG -k stop || exit
  sleep 1
  $TREEFROG -p $PORT -d || exit
  sleep 1

  # GET method
  mkdir -p $WORKDIR
  cd $WORKDIR
  wget http://localhost:$PORT/blog/index || exit
  wget http://localhost:$PORT/blog/create || exit

  # POST method
  # create1
  curl --data-urlencode 'blog[title]=hello' --data-urlencode 'blog[body]=Hello world.' http://localhost:$PORT/blog/create > create1 || exit
  [ `row_count` = "1" ] || exit

  wget http://localhost:$PORT/blog/show/1 || exit

  # create2
  curl --data-urlencode 'blog[title]=Hi!' --data-urlencode 'blog[body]=Hi, all' http://localhost:$PORT/blog/create > create2 || exit
  [ `row_count` = "2" ] || exit

  wget http://localhost:$PORT/blog/show/2 || exit

  # update
  curl --data-urlencode 'blog[id]=1' --data-urlencode 'blog[title]=Hi!' --data-urlencode "blog[body]=$RANDOMTXT" http://localhost:$PORT/blog/save/1 > save1 || exit
  [ `row_count` = "2" ] || exit

  # update check
  wget http://localhost:$PORT/blog/show/1 -O show1 || exit
  if ! grep "$RANDOMTXT" show1 >/dev/null 2>&1; then exit 1; fi

  # delete
  curl  --data-urlencode 'dummy'  http://localhost:$PORT/blog/remove/1  > remove1 || exit
  [ `row_count` = "1" ] || exit

  curl  --data-urlencode 'dummy'  http://localhost:$PORT/blog/remove/2  > remove2 || exit
  [ `row_count` = "0" ] || exit

  # API
  curl -sS http://localhost:$PORT/apibook/index | jq . || exit
  curl -sS http://localhost:$PORT/apibook/get/1 | jq . || exit
  curl -sS -X POST http://localhost:$PORT/apibook/remove/1 | jq . || exit

  # Stop app
  cd $APPROOT
  $TREEFROG -k stop || exit
}

replace()
{
  [ ! -f "$2" ] && return
  sed -e "$1" "$2" > "$2".$$
  mv -f "$2".$$ "$2"
}

test_app()
{
  cd $BASEDIR
  if [ -d "$APPNAME" ]; then
    $TREEFROG -k abort $APPNAME
    rm -rf $APPNAME
  fi

  # New application
  $TSPAWN new $APPNAME $1
  sqlite3 $DBFILE < create_blog_table.sql || exit

  # Frontend
  if [ "$2" = "with_vite+vue" ]; then
    cd $APPROOT
    yarn create vite frontend --template vue || exit
    cp ../vite.config.js frontend/
    cp ../main.js frontend/src/
    cd frontend
    yarn || exit
  fi

  cd $APPROOT
  $TSPAWN --show-tables || exit
  $TSPAWN s blog || exit
  $TSPAWN s food || exit
  $TSPAWN ms fuga || exit
  $TSPAWN w foo || exit
  $TSPAWN a book || exit
  replace "s|\[product\]|\[prod\]|" config/database.ini
  replace "s|\[dev\]|\[product\]|" config/database.ini

  if [ "$2" = "with_vite+vue" ]; then
    cd $APPROOT/frontend
    yarn build --emptyOutDir || exit
  fi

  # QMake build check
  cd $APPROOT
  qmake -r || exit
  make -j4
  make || exit
  $TREEFROG --show-routes || exit
  $TREEFROG --settings || exit

  # Test
  testcase

  cd $APPROOT
  rm -f lib/*
  make distclean

  # CMake build check
  cd $APPROOT
  cmake --version
  cmake -S . -B build
  make -j4 -C build
  make -C build || exit

  cd $APPROOT
  rm -f lib/*

  cleanup
}


## Main ##
trap 'cleanup' 2 3 15 EXIT

test_app "--template erb"
test_app "--template vue"
test_app "--template vite+vue" with_vite+vue

echo
echo "Test completed."
echo "Congratulations!"

#EOF
